<template>
	<div class="bg-white">
		<Form :model="user" :label-width="80">
			<FormItem class="w-100" label="账号">
				<Input v-model="user.account" />
			</FormItem>
			<FormItem class="w-100" label="密码">
				<Input v-model="user.password" />
			</FormItem>
			<FormItem class="w-100" label="电话">
				<Input v-model="user.tel" />
			</FormItem>
			<FormItem class="w-100" label="身份证">
				<Input v-model="user.secId" />
			</FormItem>
			<FormItem class="w-100" label="创建时间">
				<label>{{user.ct}}</label>
			</FormItem>
			<FormItem class="w-100" label="角色">
				<div v-for="(item, key) in allRoles" :key="key">
					<Checkbox @on-change="roleChange(item.roleId)" v-model="item.has">{{item.roleName}}</Checkbox>
				</div>
			</FormItem>
			

			<FormItem class="text-right">
				<Button @click="submit" type="primary">提交</Button>
			</FormItem>
		</Form>
	</div>

</template>
<script>
	export default {
		props: {
			params: {
				type: Object,
				default: undefined,
			}
		},
		data() {
			return {
				user: {
					nickName: "",
					password: "",
				},
				userId: 0,
				roles: [],
				allRoles: [],
			}
		},
		methods: {
			roleChange: function(roleId) {

			},
			submit: function() {
				let roles = [];
				for (let index in this.allRoles) {
					let roleItem = this.allRoles[index];
					if (roleItem.has) {
						roles.push(roleItem.roleId);
					}
				}
				// self.user.openId = 0;
				this.user.roles = JSON.stringify(roles);
				delete this.user.ct;
				delete this.user.ut;
				delete this.user.cu;
				delete this.user.uu;
				delete this.user.openId;
				this.http.put({
					url: "/user/info",
					param: this.user,
				}).then(() => {
					this.message.info("修改成功");
					this.$router.detail.close();
				});
			},
			init: function() {
				this.search();
			},
			searchUser: function() {

				this.http.get({
					url: "/user/info",
					param: {
						userId: this.user.userId
					}
				}).then(data => {
					this.user = data.user;
					for (let jndex in this.allRoles) {
						let role = this.allRoles[jndex];
						role.has = false;
						for (let index in data.roles) {
							let roleItem = data.roles[index];
							if (roleItem.roleId == role.roleId) {
								role.has = true;
								this.roles.push(role);
							}
						}
					}


				});
			},

			search: function() {
				this.http.get({
					url: "/role/all",
				}).then(data => {
					this.allRoles = data;
				}).then(() => {
					this.searchUser();
				});
			}

		},

		created: function() {},
		mounted: function() {
			this.user.userId = this.params.userId;
			this.init();
		}
	}
</script>

<style>
</style>